package com.coocaa.fileexplore.pages.home;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.coocaa.fileexplore.FileExploreHomeActivity;
import com.coocaa.fileexplore.R;
import com.coocaa.fileexplore.pages.home.data.FileData;
import com.coocaa.fileexplore.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by XuZeXiao on 2017/4/12.
 */

public class FileExploreHomeController implements IHomeController {
    private IHomeActivity activity = null;
    private List<FileData> datas = new ArrayList<FileData>();
    private List<File> files = new ArrayList<File>();
    private Context mContext = null;
    private FileAdapter fileAdapter = null;
    private ExecutorService executorService = null;
    private FileHandler mHandler = null;
    private Thread thread = null;
    private Boolean threadFlag = false;

    public FileExploreHomeController() {
        executorService = Executors.newSingleThreadExecutor();
        mHandler = new FileHandler();

    }

    private class FileHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    refreshData();
                    break;
            }
        }
    }

    @Override
    public void refreshList(String url) {
        FileUtils.scanFlag = false;
        if (thread != null) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (TextUtils.isEmpty(url)) {
            Log.e(FileExploreHomeActivity.TAG, "invalid url");
            return;
        }
        List<File> filesTmp = FileUtils.getFileList(url);
        if (filesTmp == null || filesTmp.size() < 0)
            return;
        if (datas == null)
            datas = new ArrayList<FileData>();
        datas.clear();
        if (files == null)
            files = new ArrayList<File>();
        files.clear();
        files.addAll(filesTmp);
        FileData dataTop = new FileData();
        dataTop.name = "..";
        datas.add(dataTop);
        if (files == null || files.size() < 0)
            return;
        for (File file : files) {
            FileData data = new FileData();
            data.name = file.getName();
            try {
                data.url = file.getCanonicalPath();
            } catch (IOException e) {
                data.url = "";
                e.printStackTrace();
            }
            data.type = file.isDirectory() ? 0 : 1;
            datas.add(data);
        }
        Collections.sort(datas, new Comparator<FileData>() {
            @Override
            public int compare(FileData fileData, FileData t1) {
                if (fileData.type > t1.type)
                    return 1;
                else if (fileData.type == t1.type)
                    return fileData.name.compareTo(t1.name);
                return -1;
            }
        });
        activity.setUrl(url);
        fileAdapter = new FileAdapter(mContext, R.layout.file_list_item, datas);
        activity.setAdapter(fileAdapter);
        getSizeInThread();
    }

    public void refreshList(int index) {
        if (datas != null && datas.size() > index) {
            FileData data = datas.get(index);
            if (data != null)
                refreshList(data.url);
        }
    }

    public void refreshData() {
        if (fileAdapter != null && mContext != null) {
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    fileAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    private void getSizeInThread() {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (datas == null || datas.size() <= 0) {
                    return;
                }
                for (FileData data : datas) {
                    try {
                        data.size = FileUtils.getFileOrFilesSize(data.url, FileUtils.SIZETYPE_MB);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Message msg = new Message();
                    msg.what = 1;
                    mHandler.sendMessage(msg);
                    if (FileUtils.scanFlag == false)
                        return;
                }
            }
        });
        FileUtils.scanFlag = true;
        thread.start();
    }

    @Override
    public void setActivity(IHomeActivity activity) {
        this.activity = activity;
        mContext = (Context) activity;
    }
}
